package com.cloudlead.common.worker.desc;

import com.cloudlead.common.worker.desc.datatype.FormData;
import com.cloudlead.common.worker.desc.datatype.MapData;
import com.cloudlead.common.worker.desc.security.SecurityDesc;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * API相关信息
 *
 * @author dean.lu
 */
@XmlRootElement
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@JsonInclude(Include.NON_NULL)
public class ActionDesc implements Comparable<ActionDesc> {

    /**
     * 动作名称
     */
    private String name;
    /**
     * 注释
     */
    private String comment;
    /**
     * 排序
     */
    private int ordinal;

    /**
     * 动作类型
     */
    private Class<?> type;
    /**
     * 所属模块
     */
    private ModuleDesc module;
    /**
     * 请求定义
     */
    private MapData request;
    /**
     * 响应定义
     */
    private FormData response;
    /**
     * 安全定义
     */
    private SecurityDesc securityDesc;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public int getOrdinal() {
        return ordinal;
    }

    public void setOrdinal(int ordinal) {
        this.ordinal = ordinal;
    }

    public Class<?> getType() {
        return type;
    }

    public void setType(Class<?> type) {
        this.type = type;
    }

    public ModuleDesc getModule() {
        return module;
    }

    public void setModule(ModuleDesc module) {
        this.module = module;
    }

    public MapData getRequest() {
        return request;
    }

    public void setRequest(MapData request) {
        this.request = request;
    }

    public FormData getResponse() {
        return response;
    }

    public void setResponse(FormData response) {
        this.response = response;
    }

    public SecurityDesc getSecurityDesc() {
        return securityDesc;
    }

    public void setSecurityDesc(SecurityDesc securityDesc) {
        this.securityDesc = securityDesc;
    }

    @Override
    public int compareTo(ActionDesc o) {
        if (null != this.getModule() && null != o.getModule()) {
            if (this.getModule().getOrdinal() == o.getModule().getOrdinal()) {
                return Integer.valueOf(this.getOrdinal()).compareTo(o.getOrdinal());
            }
            return Integer.valueOf(this.getModule().getOrdinal()).compareTo(
                    o.getModule().getOrdinal());
        } else if (null != this.getModule()) {
            return 1;
        } else {
            return -1;
        }
    }

    @Override
    public String toString() {
        return this.name;
    }

}
